import 'package:changxindianzishangwu/dao/friends_circle_dao.dart';
import 'package:changxindianzishangwu/model/friends_lately_model.dart';
import 'package:changxindianzishangwu/pages/contacts/user_cart/user_fire_page.dart';
import 'package:changxindianzishangwu/pages/find/find_page.dart';
import 'package:changxindianzishangwu/util/app_string.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/head_url.dart';
import 'package:changxindianzishangwu/util/universal_view.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:changxindianzishangwu/widget/gallery_watcher.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_user_full_info.dart';

class UserCheckPage extends StatefulWidget {
  int type;
  var userId;
  V2TimUserFullInfo userEntity;
  var ziyuId;

  UserCheckPage(
      {Key key, this.userId, this.userEntity, this.ziyuId, this.type = 1})
      : super(key: key);

  @override
  _UserCheckPageState createState() => _UserCheckPageState();
}

class _UserCheckPageState extends State<UserCheckPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, ""),
      body: Container(
        child: ListView(
          children: <Widget>[
            _headTop(context),
            _buildDynamic(),
            UniversalView().line(),
            Container(
              color: Colors.white,
              height: ScreenUtil().setHeight(55),
              child: ListTile(
                leading: Text(
                  "来源",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(15),
                  ),
                ),
                trailing: Text(
                  widget.type == 1 ? "用户ID搜索" : "扫一扫搜索",
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(15),
                      color: Color.fromRGBO(153, 153, 153, 1)),
                ),
              ),
            ),
            SizedBox(
              height: height(10),
            ),
            TextButton(
              onPressed: () {
                FocusScope.of(context).requestFocus(FocusNode());
                //todo 添加好友的时候需要两方的id
                Get.to(() => UserFirePage(
                      userId: widget.userId,
                      nickName: widget.userEntity.nickName ?? "",
                      type: widget.type,
                    ));
              },
              child: Text(
                "添加到通讯录",
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(15),
                    color: Color.fromRGBO(0, 220, 169, 1)),
              ),
              style: ButtonStyle(
                padding: MaterialStateProperty.all(EdgeInsets.all(width(15))),
                backgroundColor: MaterialStateProperty.all(Colors.white),
              ),
            ),
          ],
        ),
      ),
    );

    //   //Container(
    //     child: Stack(
    //   children: [
    //     Container(
    //       width: double.infinity,
    //       child: Positioned(
    //         child:
    //         top: ScreenUtil().setHeight(15),
    //         left: ScreenUtil().setWidth(15),
    //       ),
    //     ),
    //   ],
    // ));
  }

  ///头像 昵称 用户ID 个性签名
  Container _headTop(BuildContext context) {
    String usersign = widget.userEntity.selfSignature ?? "";
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(
          left: width(15), right: width(15), top: width(10), bottom: width(10)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              InkWell(
                onTap: () {
                  _openGalleryWatcher(context, widget.userEntity.faceUrl);
                },
                child: HeadView(widget.userEntity.faceUrl ?? HEADER_URL,
                    width(55), width(55)),
              ),
              SizedBox(
                width: width(15),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: [
                      Text(
                        widget.userEntity.nickName ?? "",
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(20),
                          color: Color.fromRGBO(51, 51, 51, 1),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 6),
                        child: Image.asset(
                          widget.userEntity.gender == 2
                              ? AppString.IMAGE_NV
                              : AppString.IMAGE_NAN,
                          height: height(12),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: height(1),
                  ),
                  Text(
                    "用户ID：" + widget.ziyuId ?? "",
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(12),
                      color: Color.fromRGBO(153, 153, 153, 1),
                    ),
                  ),
                ],
              ),
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: 6),
            child: UniversalView().line(indent: 70),
          ),
          // 签名
          Container(
            margin: EdgeInsets.only(
                left: width(70), right: width(15), top: height(10)),
            child: Text(
              usersign,
              textAlign: TextAlign.left,
              style: TextStyle(
                fontSize: ScreenUtil().setSp(12),
                color: Color.fromRGBO(51, 51, 51, 1),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // Container _headTop1() {
  //   return Container(
  //     color: Colors.red,
  //     child: Column(
  //       children: <Widget>[
  //         InkWell(
  //           onTap: () {},
  //           child: Row(
  //             children: <Widget>[
  //               Container(
  //                 width: ScreenUtil().setWidth(55),
  //                 height: ScreenUtil().setWidth(55),
  //                 decoration: BoxDecoration(
  //                   image: DecorationImage(
  //                       image: CachedNetworkImageProvider(
  //                         widget.userEntity.faceUrl ?? HEADER_URL,
  //                       ),
  //                       fit: BoxFit.cover),
  //                   borderRadius: BorderRadius.circular(ScreenUtil().radius(6)),
  //                 ),
  //                 child: Text(""),
  //                 margin: EdgeInsets.only(
  //                   top: ScreenUtil().setHeight(15),
  //                   left: ScreenUtil().setWidth(15),
  //                 ),
  //               ),
  //               Container(
  //                 width: ScreenUtil().setWidth(150),
  //                 child: Column(
  //                   children: <Widget>[
  //                     Container(
  //                       height: ScreenUtil().setHeight(28),
  //                       margin: EdgeInsets.only(
  //                         top: 15,
  //                         left: ScreenUtil().setWidth(10),
  //                       ),
  //                       alignment: Alignment.centerLeft,
  //                       child: Text(
  //                         widget.userEntity.nickName ?? "",
  //                         style: TextStyle(
  //                           fontSize: ScreenUtil().setSp(20),
  //                           color: Color.fromRGBO(51, 51, 51, 1),
  //                         ),
  //                       ),
  //                     ),
  //                     Container(
  //                       margin: EdgeInsets.only(
  //                           left: ScreenUtil().setWidth(10),
  //                           top: ScreenUtil().setHeight(1)),
  //                       alignment: Alignment.centerLeft,
  //                       child: Row(
  //                         children: [
  //                           Text(
  //                             "用户ID：" + widget.ziyuId ?? "",
  //                             style: TextStyle(
  //                               fontSize: ScreenUtil().setSp(12),
  //                               color: Color.fromRGBO(153, 153, 153, 1),
  //                             ),
  //                           ),
  //                           Padding(
  //                             padding: const EdgeInsets.only(left: 6),
  //                             child: Image.asset(
  //                               widget.userEntity.gender == 2
  //                                   ? AppString.IMAGE_NV
  //                                   : AppString.IMAGE_NAN,
  //                               height: height(12),
  //                             ),
  //                           )
  //                         ],
  //                       ),
  //                     ),
  //                   ],
  //                 ),
  //               ),
  //             ],
  //           ),
  //         ),
  //         Container(
  //           margin: EdgeInsets.only(top: ScreenUtil().setHeight(6)),
  //           child: Divider(
  //             height: 1,
  //             indent: ScreenUtil().setHeight(80),
  //             color: Color.fromRGBO(248, 248, 248, 1),
  //             thickness: ScreenUtil().setHeight(1),
  //           ),
  //         ),
  //         // 签名
  //         Container(
  //           margin: EdgeInsets.only(left: width(80), right: width(15)),
  //           padding: EdgeInsets.only(top: width(10), bottom: width(10)),
  //           alignment: Alignment.topLeft,
  //           child: Text(
  //             widget.userEntity.selfSignature ?? "",
  //             textAlign: TextAlign.left,
  //             maxLines: 2,
  //             style: TextStyle(
  //               fontSize: ScreenUtil().setSp(12),
  //               color: Color.fromRGBO(51, 51, 51, 1),
  //             ),
  //           ),
  //         ),
  //       ],
  //     ),
  //   );
  // }

  InkWell _buildDynamic() {
    return InkWell(
      onTap: () {
        Get.to(() => FindPage(
              widget.userId,
              isOnlyUser: true,
            ));
      },
      child: Container(
          margin: EdgeInsets.only(top: 10),
          color: Colors.white,
          height: 60,
          padding: EdgeInsets.only(
              left: width(15),
              right: width(15),
              top: width(5),
              bottom: width(5)),
          child: Row(
            children: [
              Text(
                "动态",
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(15),
                ),
              ),
              Expanded(
                flex: 1,
                child: Container(
                    margin: EdgeInsets.only(left: ScreenUtil().setWidth(1)),
                    child: getLateLyBuilder()),
              ),
              Icon(
                Icons.keyboard_arrow_right,
                color: Colors.grey,
              )
            ],
          )),
    );
  }

  @override
  void initState() {
    print('二维码-------UserCheckPage initState-------');
    super.initState();
    //_getData();
  }

  @override
  void dispose() {
    print('二维码-------UserCheckPage dispose-------');

    super.dispose();
    // TencentImPlugin.removeListener(_imListener);
  }

  getLateLyBuilder() {
    return FutureBuilder(
        future: getLateLy(),
        builder: (context, sp) {
          if (sp.connectionState == ConnectionState.done && sp.data != null) {
            List<LatelyBean> data = sp.data;
            return ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: data.length,
                itemBuilder: (context, int index) {
                  return Container(
                    margin: EdgeInsets.only(left: ScreenUtil().setWidth(8)),
                    child: HeadView(
                      data[index].type == 2 && data[index].url != null
                          ? data[index].url
                          : BACKGROUND_IMAGE,
                      width(50),
                      width(50),
                    ),
                  );
                });
          } else {
            return SizedBox();
          }
        });
  }

  getLateLy() async {
    return await FriendsCircleDao.getFriendsLately(widget.userId);
  }

  _openGalleryWatcher(BuildContext context, String path) {
    List<String> artImage = [];
    artImage.add(path);
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => GalleryPhotoViewWrapper(
          thumbGalleryItems: artImage,
          tagItems: artImage,
          backgroundDecoration: const BoxDecoration(
            color: Colors.black,
          ),
          initialIndex: 1,
          scrollDirection: Axis.horizontal,
        ),
      ),
    );
  }
}
